
　　const express = require('express')
    var escape = require('escape-html');
　　const app = express()  

    const fs = require('fs')

　　app.get('/', (req, res) => {
    res.cookie('cookie', '123123123123');
    // res.cookie('cookie', '123123123123', {httpOnly: true });
    fs.readFile('./qwe.html',(err, data)=>{
        res.end(data)
      })   
    })  

    var content = "";

    app.post('/test', function (req, res, next) {
      content=req.query.test
    })
    
    app.get("/content",(req,res)=>{
        // 解决方案：
        // res.send(escape(content))
        // res.send(htmlEscape(content))
        res.send(content)
    })

    app.listen(3000)


    //转义方法
    function htmlEscape(text){
      return text.replace(/[<>"&]/g, function(match, pos, originalText){
        switch(match){
        case "<": return "&lt;";
        case ">":return "&gt;";
        case "&":return "&amp;";
        case "\"":return "&quot;";
      }
      });
    }



    //sql注入


    const bodyParser = require("body-parser"); 
    // 解析 application/x-www-form-urlencoded
    var urlencodedParser = bodyParser.urlencoded({ extended: false });
    // 解析 application/json
    app.use(bodyParser.json())  

    var mysql = require('mysql');
    var db = mysql.createConnection({
      host: 'localhost', //数据库地址
      port: '3306',//端口号
      user: 'root',//用户名
      password: 'ynr666666',//密码
      database: 'xsssq'//数据库名称
  });
  db.connect();

// app.post("/login",urlencodedParser,(req,res)=>{
//   var sql = `select * from pet where username='${req.body.username}' and password='${req.body.password}'`
//   var data = '';
//   db.query(sql, function(err, result) {
//   data = Array.from(result);
//   if(data && data.length>0)
//   res.send("登录成功")
//   else
//   res.send("登录失败")
// })
// })

// 1' or '1'='1


//存储型xss
 const atob = require('atob')  
 app.post("/save",urlencodedParser,async (req,res)=>{
  //  console.log(atob(req.body.xss1),6766)
   let sqlInsert = `insert into goodsgoods(name,price) value('${req.body.xss1}',"55")`
   db.query(sqlInsert, function(err, result) {
    res.send(result)
 })
})
app.get("/check",urlencodedParser,async (req,res)=>{
  let sql = `select * from goodsgoods`
  db.query(sql, function(res2, result) {
    res.send(result)
 })
})
//<script>alert(document.cookie);</script>
